package com.edu.zut.hb.software.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.zut.hb.software.server.pojo.Student;
import com.edu.zut.hb.software.server.pojo.UpdateVo.StudentUpdateVo;
import com.edu.zut.hb.software.server.pojo.registerVo.StudentRegisterVo;
import com.edu.zut.hb.software.server.pojo.RespBean;
import com.edu.zut.hb.software.server.service.IStudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2021-08-15
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @ApiOperation(value = "获取所有学生")
    @GetMapping("/")
    public List<Student> getAllStudent() {
        return studentService.list(new QueryWrapper<Student>().last("limit 50"));
    }

    @ApiOperation(value = "添加学生")
    @PostMapping("/add")
    public RespBean addStudent(@RequestBody StudentRegisterVo registerVo) {
        if (studentService.save(new Student(registerVo))) {
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "删除学生")
    @DeleteMapping("/{id}")
    public RespBean deletStudent(@PathVariable String id) {
        if (studentService.removeById(id)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "更新学生")
    @PutMapping("/update")
    public RespBean updateStudent(@RequestBody StudentUpdateVo updateVo) {
        if (studentService.updateById(new Student(updateVo))) {
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "获取单个学生信息")
    @GetMapping("/{name}")
    public List<Student> getOneStudent(@PathVariable String name) {
        return studentService.getOneStudent(name);
    }


}
